package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.draft.pojo.dto.DraftDeleteDTO;
import com.xbongbong.pro.draft.pojo.dto.DraftGetDTO;
import com.xbongbong.pro.draft.pojo.dto.DraftSaveDTO;
import com.xbongbong.pro.draft.pojo.vo.DraftDeleteVO;
import com.xbongbong.pro.draft.pojo.vo.DraftGetVO;
import com.xbongbong.pro.draft.pojo.vo.DraftSaveVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.service.FormDataDraftService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 表单草稿相关接口
 *
 * @author GuoJun.HU
 * @date 2019/2/20 19:06
 * @since V1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/form/data/draft")
public class FormDataDraftController {

    @Resource
    private FormDataDraftService formDataDraftService;

    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String readAll(@RequestBody @Valid DraftSaveDTO draftSaveDTO, BindingResult br) {
        XbbResponse<DraftSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DraftSaveVO draftSaveVO = formDataDraftService.save(draftSaveDTO);
                response = new XbbResponse<>(draftSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String unreadCount(@RequestBody @Valid DraftGetDTO draftGetDTO, BindingResult br) {
        XbbResponse<DraftGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DraftGetVO draftGetVO = formDataDraftService.get(draftGetDTO);
                response = new XbbResponse<>(draftGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String groupList(@RequestBody @Valid DraftDeleteDTO draftDeleteDTO, BindingResult br) {
        XbbResponse<DraftDeleteVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DraftDeleteVO draftDeleteVO = formDataDraftService.delete(draftDeleteDTO);
                response = new XbbResponse<>(draftDeleteVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
